package com.xixi.core.rpc.fault.retry;

import com.github.rholder.retry.*;
import com.xixi.core.rpc.model.RpcResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @author: zxh
 * @date: 2024/8/27 15:28
 * @description: 固定时间间隔
 */
@Slf4j
public class FixedIntervalRetryStrategy implements RetryStrategy{
	@Override
	public RpcResponse doRetry(Callable<RpcResponse> callable) throws Exception {
		Retryer<RpcResponse> retryer = RetryerBuilder.<RpcResponse>newBuilder()
				.retryIfExceptionOfType(Exception.class)
				.withWaitStrategy(WaitStrategies.fixedWait(3L, TimeUnit.SECONDS))
				.withStopStrategy(StopStrategies.stopAfterAttempt(3))
				.withRetryListener(new RetryListener() {
					@Override
					public <V> void onRetry(Attempt<V> attempt) {
						log.info("重试次数 {}", attempt.getAttemptNumber());
					}
				})
				.build();
		return retryer.call(callable);
	}
}
